//
//  THScratchCardView.m
//  THImageEditDemo
//
//  Created by 希达 on 2018/4/8.
//  Copyright © 2018年 Tan.huang. All rights reserved.
//

#import "THScratchCardView.h"


@interface THCanvasView ()

@property (assign, nonatomic) CGFloat lineWidth;
@property (strong, nonatomic) UIColor *strokeColor;

@property (nonatomic, assign)  CGPoint currentPoint;
@property (nonatomic, assign) CGPoint previousPoint1;
@property (nonatomic, assign) CGPoint previousPoint2;


@property (nonatomic, assign) CGContextRef context;

@property (assign, nonatomic) BOOL  isErase;
@end

@implementation THCanvasView

- (void)dealloc {
    NSLog(@"%@释放了", NSStringFromClass(self.class));
}

CGPoint midPoint1(CGPoint p1, CGPoint p2)
{
    return CGPointMake((p1.x + p2.x) * 0.5, (p1.y + p2.y) * 0.5);
}


- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];
    self.previousPoint1 = [touch locationInView:self];
    self.previousPoint2 = [touch locationInView:self];
    self.currentPoint = [touch locationInView:self];

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, self.previousPoint1.x, self.previousPoint1.y);

}

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    UITouch *touch  = [touches anyObject];
    self.previousPoint2  = self.previousPoint1;
    self.previousPoint1  = [touch previousLocationInView:self];
    self.currentPoint    = [touch locationInView:self];

    CGPoint mid1    = midPoint1(self.previousPoint1, self.previousPoint2);
    CGPoint mid2    = midPoint1(self.currentPoint, self.previousPoint1);
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, mid1.x, mid1.y);
    CGPathAddQuadCurveToPoint(path, NULL, self.previousPoint1.x, self.previousPoint1.y, mid2.x, mid2.y);

    CGRect bounds = CGPathGetBoundingBox(path);
    CGPathRelease(path);
    CGRect drawBox = bounds;

    //Pad our values so the bounding box respects our line width
    drawBox.origin.x        -= self.lineWidth * 2;
    drawBox.origin.y        -= self.lineWidth * 2;
    drawBox.size.width      += self.lineWidth * 4;
    drawBox.size.height     += self.lineWidth * 4;

    UIGraphicsBeginImageContext(drawBox.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIGraphicsEndImageContext();

    [self setNeedsDisplayInRect:drawBox];
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    UITouch *touch  = [touches anyObject];
    self.currentPoint = [touch locationInView:self];
    self.previousPoint2  = self.previousPoint1;
    self.previousPoint1  = [touch previousLocationInView:self];

    CGPoint mid1    = midPoint1(self.previousPoint1, self.previousPoint2);
    CGPoint mid2    = midPoint1(self.currentPoint, self.previousPoint1);

    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, mid1.x, mid1.y);
    CGPathAddQuadCurveToPoint(path, NULL, self.previousPoint1.x, self.previousPoint1.y, mid2.x, mid2.y);

    //绘画
    CGRect bounds = CGPathGetBoundingBox(path);
    CGPathRelease(path);
    CGRect drawBox = bounds;

    //Pad our values so the bounding box respects our line width
    drawBox.origin.x        -= self.lineWidth * 2;
    drawBox.origin.y        -= self.lineWidth * 2;
    drawBox.size.width      += self.lineWidth * 4;
    drawBox.size.height     += self.lineWidth * 4;

    UIGraphicsBeginImageContext(drawBox.size);
    [self.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIGraphicsEndImageContext();

    [self setNeedsDisplayInRect:drawBox];
}

- (void)drawRect:(CGRect)rect {

    //获取上下文

    CGPoint mid1 = midPoint1(self.previousPoint1, self.previousPoint2);
    CGPoint mid2 = midPoint1(self.currentPoint, self.previousPoint1);

    //
    self.context = UIGraphicsGetCurrentContext();

    [self.layer renderInContext:self.context];

    CGContextMoveToPoint(self.context, mid1.x, mid1.y);

    // CGPathAddQuadCurveToPoint
    CGContextAddQuadCurveToPoint(self.context, self.previousPoint1.x, self.previousPoint1.y, mid2.x, mid2.y);

    CGContextSetLineCap(self.context, kCGLineCapRound);

    CGContextSetLineWidth(self.context, self.lineWidth);

    CGContextSetStrokeColorWithColor(self.context, self.strokeColor.CGColor);

    CGContextSetLineJoin(self.context, kCGLineJoinRound);

    CGContextSetBlendMode(self.context, self.isErase ? kCGBlendModeDestinationIn:kCGBlendModeNormal);

    CGContextStrokePath(self.context);

    [super drawRect:rect];
}

@end




@implementation THScratchCardView

- (void)dealloc {
    NSLog(@"%@释放了", NSStringFromClass(self.class));
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self configureView];
    }
    return self;
}



- (void)configureView {
    self.contentViewContainer = [[UIView alloc] initWithFrame:self.bounds];
    self.coverViewContainer = [[UIView alloc] initWithFrame: self.bounds];
    self.canvasMaskView = [[THCanvasView alloc] initWithFrame:self.bounds];

    [self configureMaskView];
    
    self.coverViewContainer.backgroundColor = [UIColor clearColor];
    [self addSubview:self.coverViewContainer];

    self.contentViewContainer.backgroundColor = [UIColor clearColor];
    [self addSubview:self.contentViewContainer];

}

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    return  self.canvasMaskView;
}

- (void)configureMaskView {
    _canvasMaskView.backgroundColor = [UIColor clearColor];
    [self.contentViewContainer setMaskView:self.canvasMaskView];
}

- (void)setStrokeColor:(UIColor *)strokeColor {
    _strokeColor = strokeColor;
    self.canvasMaskView.strokeColor = strokeColor;
}

- (void)setLineWidth:(NSInteger)lineWidth {
    _lineWidth = lineWidth;
    _canvasMaskView.lineWidth = lineWidth;
}

- (void)setIsErase:(BOOL)isErase {
    _isErase = isErase;
    _canvasMaskView.isErase = isErase;
}



@end
